// 礼物管理

var constant = require('../../const');
var models = require('../../models');
var fs = require('fs');

function getGift(req,res){
	if (!req.AV.user) {
    res.redirect('/admin');
  }

  // 0是未上架, 1 已经上架的
  var query = new AV.Query(models.MLGift);
  query.descending("priority");
  query.find({
    success:function(gifts) {
      var data = { "username": req.session.username, "gifts": gifts};
      res.render('admin/gift/index', data);
    },
    error: function(error) {
      res.render('admin/gift/index', {"username": req.session.username});
    }
  });
}

function getGiftEdit(req,res){
	if (!req.AV.user) {
    res.redirect('/admin');
  }

  var objectId = req.query.objectId;
  // 编辑专题
  var giftQuery = new AV.Query(models.MLGift);
  giftQuery.get(objectId, {
    success: function(gift) {
      var data = {
        "username": req.session.username,
        "gift": gift,
      };
      res.render('admin/gift/edit', data);
    },
    error: function(error) {
      res.redirect("/admin/gift");
    }
  });
}

function giftEdit(req,res){
	if (!req.AV.user) {
    res.redirect('/admin');
  }

  var name = req.body.name;
  var priority=req.body.priority||0;
  var price = req.body.price;
  var info = req.body.info;
  var giftImg = req.files.giftImg;
  var objectId = req.body.objectId;

  // 编辑专题
  var giftQuery = new AV.Query(models.MLGift);
  giftQuery.get(objectId).then(function(gift) {
    // 如果有新的照片
    if(giftImg.name) {
      // 要求上传图片
      fs.readFile(giftImg.path, function(err, imgData) {
        if (err) {
          var data = {
            "username": req.session.username,
            "errMsg": "图片保存失败, 请重试",
          };
          res.render('admin/gift/add', data);
        } else {
          // 保存图片
          var avFile = new AV.File(giftImg.name, imgData);
          avFile.save().then(function() {
            // 文件保存成功, 新增专题
            gift.set("name", name);
            gift.set("priority",parseInt(priority));
            gift.set("price",parseInt(price));
            gift.set("info",info);
            gift.set("img", avFile);
            gift.save({
              success: function() {
                res.redirect('admin/gift');
              },
              error: function(error) {
                res.redirect('admin/gift');
              }
            });

          }, function(error) {
            // 文件保存错误
            var data = {
              "username": req.session.username,
              "errMsg": "图片保存失败, 请重试",
            };
            res.render('admin/gift/add', data);

          });
        }
      });
    } else {
      // 只保存标题
      gift.set("name", name);
      gift.set("priority",parseInt(priority));
      console.log(price);
      gift.set("price",parseInt(price));
      gift.set("info",info);
      gift.save({
        success: function() {
          res.redirect('admin/gift');
        },
        error: function(gift ,error) {
          console.log(error)
          res.redirect('admin/gift');
        }
      });
    }
  }, function(error) {
    res.redirect('admin/gift');
  });
}

function getGiftAdd(req,res){
	if (!req.AV.user) {
    res.redirect('/admin');
  }

  // 新增礼物
  var data = {
    "username": req.session.username,
  };
  res.render('admin/gift/add', data);
}

//新增礼物post
function giftAdd(req,res){
	if (!req.AV.user) {
    res.redirect('/admin');
  }

  var name = req.body.name;
  var priority=req.body.priority||0;
  var info = req.body.info||0;
  var price = req.body.price||"";
  var giftImg = req.files.giftImg;

  // 上传图片
  if(giftImg.name) {
    // 要求上传图片
    fs.readFile(giftImg.path, function(err, imgData) {
      if (err) {
        var data = {
          "username": req.session.username,
          "errMsg": "图片保存失败, 请重试",
        };
        res.render('admin/gift/add', data);
      } else {
        // 保存图片
        var avFile = new AV.File(giftImg.name, imgData);
        avFile.save().then(function() {
          // 文件保存成功, 新增礼物
          var gift = new models.MLGift;
          gift.set("name",name);
          gift.set("status", 0);
          gift.set("priority",parseInt(priority));
          gift.set("price",parseInt(price));
          gift.set("info",info);
          gift.set("img", avFile);
          gift.save({
            success: function() {
              res.redirect('admin/gift');
            },
            error: function(error) {
              res.redirect('admin/gift');
            }
          });

        }, function(error) {
          // 文件保存错误
          var data = {
            "username": req.session.username,
            "errMsg": "图片保存失败, 请重试",
          };
          res.render('admin/gift/add', data);

        });
      }
      
    });
    
  } else {
    // 返回错误
    var data = {
      "username": req.session.username,
      "errMsg": "请添加一张标题图片",
    };
    res.render('admin/gift/add', data);
  }
}

/*
 * title: 发布礼物
 */
function publishGift(req, res) {
  if (!req.AV.user) {
    res.redirect('/admin');
  }

  var objectId = req.body.objectId;
  console.log(objectId)
  // 编辑专题
  var giftQuery = new AV.Query(models.MLGift);
  giftQuery.get(objectId, {
    success: function(gift) {
      gift.set("status", 1);
      gift.save().then(function(obj) {
        res.send({"code":0,"msg":"成功"});
      });
    },
    error: function(error) {
      res.send({"code":1,"msg":"失败"});
    }
  });
}



/*
 * title: 禁用专题
 */
function disableGift(req, res) {
  if (!req.AV.user) {
    res.redirect('/admin');
  }

  var objectId = req.body.objectId;
  // 编辑专题
  var giftQuery = new AV.Query(models.MLGift);
  giftQuery.get(objectId, {
    success: function(gift) {
      gift.set("status", 0);
      gift.save().then(function(obj) {
        res.send({"code":0,"msg":"成功"});
      });
    },
    error: function(error) {
      res.send({"code":1,"msg":"失败"});
    }
  });
}


exports.getGift = getGift;
exports.getGiftEdit = getGiftEdit;
exports.giftEdit = giftEdit;
exports.getGiftAdd = getGiftAdd;
exports.giftAdd = giftAdd;
exports.publishGift = publishGift;
exports.disableGift = disableGift;
